package com.example.design.c.state;

/**
 * 状态模式
 */
public class StatePatternClient {

    public static void main(String[] args) {
        Context context = new Context();
        context.Handle();//处理请求；
        State stateB = new ConcreteStateB();
        context.setState(stateB);
        context.Handle();
        State stateA =new ConcreteStateA();
        context.setState(stateA);
        context.Handle();
        context.setState(stateB);
        context.Handle();
    }
}

class Context {
    private State state;

    //定义环境类的初始状态
    public Context() {
        state = new ConcreteStateA();
    }

    public State getState() {
        return state;
    }

    //设置新状态
    public void setState(State state) {
        this.state = state;
    }

    //对请求做处理
    public void Handle(){
        state.handle(this);
    }
}

//抽象状态类
abstract class State{

    public abstract void handle(Context context);
}

//具体状态类A
class ConcreteStateA extends State{

    @Override
    public void handle(Context context) {
        System.out.println("当前状态是：A");
    }
}

class ConcreteStateB extends State {

    @Override
    public void handle(Context context) {
        System.out.println("当前状态是：B");
    }
}